﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using DBUtility;
namespace OracleDAL.Sec
{

    public class LogonLog
    {
        /// <summary>
        /// 把日志写入数据库
        /// </summary>
  
        public bool WriteLog(Model.Sec.LogonLog log)
        {
            string sql = "INSERT INTO SEC_LOGONLOG(LOGISN,USERISN,USERNAME,USERIP,USERMAC,LOGONTIME,LOGOUTTIME,LOGONSUCCEED) ";
            sql += "values('" + log.LogISN + "','" + log.UserISN + "','" + log.UserName + "','" + log.UserIP + "','" + log.UserMAC + "',to_date('" + log.LogonTime + "','yyyy-MM-dd hh24:mi:ss'),to_date('" + log.LogoutTime + "','yyyy-MM-dd hh24:mi:ss'),'" + log.LogonSucceed + "')";
            if (DBUtility.OracleHelper.ExecuteSql(sql) > 0)
                return true;
            else
                return false;
        }
        /// <summary>
        /// 写入登录日志 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Add(Model.Sec.LogonLog model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into SEC_LOGONLOG(");
            strSql.Append("LOGISN,USERISN,USERNAME,USERIP,USERMAC,LOGONTIME,LOGOUTTIME,LOGONSUCCEED)");
            strSql.Append(" values (");
            strSql.Append(":LOGISN,:USERISN,:USERNAME,:USERIP,:USERMAC,:LOGONTIME,:LOGOUTTIME,:LOGONSUCCEED)");
            OracleParameter[] parameters = {
					new OracleParameter(":LOGISN", OracleType.VarChar,50),
					new OracleParameter(":USERISN", OracleType.VarChar,50),
					new OracleParameter(":USERNAME", OracleType.VarChar,50),
					new OracleParameter(":USERIP", OracleType.VarChar,20),
					new OracleParameter(":USERMAC", OracleType.VarChar,50),
					new OracleParameter(":LOGONTIME", OracleType.DateTime),
					new OracleParameter(":LOGOUTTIME", OracleType.DateTime),
					new OracleParameter(":LOGONSUCCEED", OracleType.VarChar,20)};
            parameters[0].Value = model.LogISN;
            parameters[1].Value = model.UserISN;
            parameters[2].Value = model.UserName;
            parameters[3].Value = model.UserIP;
            parameters[4].Value = model.UserMAC;
            parameters[5].Value = model.LogonTime;
            parameters[6].Value = model.LogoutTime;
            parameters[7].Value = model.LogonSucceed;
            return OracleHelper.ExecuteSql(strSql.ToString(), parameters) > 0 ? true : false;

        }
        /// <summary>
        /// 更新登录日志退出时间,退出时间就是执行时间
        /// </summary>
        /// <param name="logISN"></param> 
        /// <returns></returns>
        public bool Update(string logISN)
        {
            string sql = "update SEC_LOGONLOG set LOGOUTTIME=sysdate where LOGISN='" + logISN + "'";
            return OracleHelper.ExecuteSql(sql) > 0 ? true : false;
        }
        /// <summary>
        /// 返回MODEL
        /// </summary>
        /// <param name="LogISN"></param>
        /// <returns></returns>
        public Model.Sec.LogonLog GetModel(string LogISN)
        {
            string sql = "SELECT * FROM SEC_LOGLONLOG WHERE LOGISN='"+LogISN+"'";
            System.Data.DataTable dt = DBUtility.OracleHelper.Query(sql).Tables[0];
            if (dt.Rows.Count > 0)
            {
                Model.Sec.LogonLog logonLog = new Model.Sec.LogonLog();
                logonLog.LogISN = LogISN;
                logonLog.LogonSucceed = dt.Rows[0]["LOGONSUCCEED"].ToString();
                logonLog.LogonTime = Convert.ToDateTime(dt.Rows[0]["LOGONTIME"]);
                logonLog.LogoutTime = Convert.ToDateTime(dt.Rows[0]["LOGOUTTIME"]);
                logonLog.UserIP = dt.Rows[0]["USERIP"].ToString();
                logonLog.UserISN = dt.Rows[0]["USERISN"].ToString();
                logonLog.UserMAC = dt.Rows[0]["USERMAC"].ToString();
                logonLog.UserName = dt.Rows[0]["USERNAME"].ToString();
                return logonLog;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 删除登录记录
        /// </summary>
        /// <param name="strWhere">条件</param>
        /// <returns></returns>
        public bool Delete(string strWhere)
        {
            string sql = "delete from sec_logonlog where " + strWhere;
            return OracleHelper.ExecuteSql(sql) > 0 ? true : false;
        }

        /// <summary>
        /// 根据条件获得登录记录集
        /// </summary>
        /// <param name="strWhere">条件,可以为空</param>
        /// <returns></returns>
        public DataTable GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select LOGISN,USERISN,USERNAME,USERIP,USERMAC,LOGONTIME,LOGOUTTIME,LOGONSUCCEED ");
            strSql.Append(" FROM SEC_LOGONLOG ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return OracleHelper.Query(strSql.ToString()).Tables[0];
        }
    }
}
